package com.blog.blogservice.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.blogservice.app.service.RoleService;
import com.blog.blogservice.app.service.RoleUserService;
import com.blog.blogservice.app.service.UserService;
import com.blog.blogservice.domain.entity.Role;
import com.blog.blogservice.domain.entity.RoleUser;
import com.blog.blogservice.domain.entity.UserEntity;
import com.blog.blogservice.infra.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色表(Role)应用服务
 *
 * @author guanglin.ma
 * @since 2024-03-10 22:54:10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleUserService roleUserService;

    @Override
    public List<Role> selectRoleByUserName(UserEntity userEntity) {

        //查询用户信息Id
        userEntity = userService.selectUser(userEntity);
        RoleUser roleUser = new RoleUser();
        roleUser.setUserId(userEntity.getUserId());
        roleUser.setRoleId(userEntity.getUserId());
        //查询用户角色关联
        List<RoleUser> roleUserList = roleUserService.selectRoleUser(roleUser);
        List<Integer> roleIds = roleUserList.stream().map(RoleUser::getRoleId).collect(Collectors.toList());
        List<Role> roleList= baseMapper.selectBatchIds(roleIds);
        return roleList;
    }
}

